<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"
      :label-width="$i18n.locale === 'en-US' ? '140px' : '130px'">
      <el-row>
        <el-col :span="12">
          <el-form-item label="设备名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="设备名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备序列号" prop="en">
            <el-input v-model="dataForm.en" placeholder="设备序列号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备关联的类型" prop="linkType">
            <el-select v-model="dataForm.linkType" style="width: 100%" placeholder="门票类型" clearable
              @change="changeLinkType">
              <el-option v-for="item in linkTypeOptions" :key="item.dictValue" :label="item.dictLabel"
                :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="关联的类型名称" prop="linkId">
            <el-select v-model="dataForm.linkId" style="width: 100%" placeholder="景区" clearable>
              <el-option v-for="item in linkNameOptions" :key="dataForm.linkType == 2 ? item.portNum : item.id" :label="dataForm.linkType == 1
                ? item.scenicName
                : dataForm.linkType == 2
                  ? item.portName
                  : item.name
                " :value="dataForm.linkType == 2 ? item.portNum : item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备类型" prop="type">
            <el-radio-group v-model="dataForm.type">
              <el-radio :label="1">手持扫描枪</el-radio>
              <el-radio :label="2">闸机</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备状态" prop="status">
            <el-radio-group v-model="dataForm.status">
              <el-radio :label="0">未授权</el-radio>
              <el-radio :label="1">授权</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备安放区域" prop="location">
            <el-input v-model="dataForm.location" placeholder="设备安放区域"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      sceid: "",
      linkNameOptions: [],
      scenicOptions: [],
      linkTypeOptions: [
        {
          dictValue: 1,
          dictLabel: "景区门票",
        },
        {
          dictValue: 2,
          dictLabel: "船票",
        },
        {
          dictValue: 3,
          dictLabel: "渔家乐",
        },
      ],
      visible: false,
      dataForm: {
        id: "",
        en: "",
        linkId: "",
        linkName: "",
        linkType: "",
        location: "",
        name: "",
        remark: "",
        status: "",
        type: "",
      },
    };
  },
  created() { },
  mounted() { },
  watch: {
    "dataForm.linkType"(newVal, oldVal) {
      //   console.log("这里", newVal, oldVal);
      if (newVal && newVal != oldVal) {
        // this.dataForm.linkId = "";
        // console.log("this.dataForm：", this.dataForm);
        this.getLinkNameOptions();
      } else {
        this.dataForm.linkId = "";
        this.linkNameOptions = [];
      }
    },
  },
  computed: {
    dataRule() {
      return {
        name: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("validate.required"),

          },
        ],
        linkId: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("validate.required"),

          },
        ],
        type: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("validate.required"),

          },
        ],
      };
    },
  },
  methods: {
    changeLinkType(e) {
      this.dataForm.linkId = "";
      //   this.getLinkNameOptions();
    },
    getLinkNameOptions() {
      let url = "";
      if (this.dataForm.linkType == 1) {
        url = "scenic/isscenicspot/queryChargeSpots";
      } else if (this.dataForm.linkType == 2) {
        url = "/travel/isshipport/listDistinctPort";
      } else if (this.dataForm.linkType == 3) {
        url = "scenic/fisherman/page";
      }
      this.$http
        .get(url, {
          page: 1,
          limit: 100,
        })
        .then(({ data: res }) => {
          if (res.msg == "success") {
            this.linkNameOptions =
              this.dataForm.linkType == 3 ? res.data.list : res.data;
            // console.log("this.linkNameOptions：", this.linkNameOptions);
          }
        });
    },
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get("/order/dm/" + this.dataForm.id)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
        })
        .catch(() => { });
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          let keyName =
            this.dataForm.linkType == 1
              ? "scenicName"
              : this.dataForm.linkType == 2
                ? "portName"
                : "name";
          let keyId = this.dataForm.linkType == 2 ? "portNum" : "id";
          let linkNameObj = this.linkNameOptions.filter(
            (item) => item[keyId] === this.dataForm.linkId
          );
          if (linkNameObj.length != 0) {
            this.dataForm.linkName = linkNameObj[0][keyName];
          }
          // console.log("提交数据", this.dataForm);
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/order/dm",
            this.dataForm
          )
            .then(({ data: res }) => {
              // console.log("res", res);
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch((err) => {
              console.log("err:", err);
            });
        });
      },
      1000,
      {
        leading: true,
        trailing: false,
      }
    ),
  },
};
</script>
